package subraSoft.centaurus.gui.components;

import java.awt.CardLayout;
import java.awt.Component;

import javax.swing.JPanel;

/**
 * Inplementa un panel con muchos paneles dentro pero solo uno visible a la vez.
 * 
 * @author christian
 */
public class CenViewStack extends JPanel 
{
	private static final long serialVersionUID = 1L;
	private CardLayout cards;
	private int selectedIndex = 0;
	
	/**
	 * Constructor.
	 */
	public CenViewStack()
	{
		cards = new CardLayout();
		setLayout(cards);
	}

	@Override
	public Component add(Component comp, int index) {
		throw new RuntimeException("No permitido, usar add(comp)");
	}

	@Override
	public void add(Component comp, Object constraints, int index) {
		throw new RuntimeException("No permitido, usar add(comp)");
	}

	@Override
	public void add(Component comp, Object constraints) {
		throw new RuntimeException("No permitido, usar add(comp)");
	}

	@Override
	public Component add(Component comp)
	{
		super.add(comp, Integer.toString(getComponentCount()));
		return comp;
	}

	@Override
	public Component add(String name, Component comp) {
		throw new RuntimeException("No permitido, usar add(comp)");
	}
	
	/**
	 * Obtiene el inidice de panel seleccionado.
	 * 
	 * @return el inidice de panel seleccionado.
	 */
	public int getSelectedIndex() {
		return selectedIndex;
	}
	
	/**
	 * Setea el indice de panel seleccionado.
	 * 
	 * @param selectedIndex el indice de panel seleccionado.
	 */
	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
		cards.show(this, Integer.toString(selectedIndex));
	}
	
	/**
	 * Obtiene el panel seleccionado.
	 * 
	 * @return el panel seleccionado.
	 */
	public Component getSelectedComponent()
	{
		return getComponent(selectedIndex);
	}
	
}
